/**
 * 水果爆炸单个物体
 */
class fruitBoomItem extends BaseComp{
    private fruit_left:eui.Image;
    private fruit_right:eui.Image;
    private img_di:eui.Image;
    private img_boom:eui.Image;
    private boom_hide:eui.Image;
    private img_kac:eui.Image;
    private dot_mc:egret.MovieClip;

    public static boomItemPool:fruitBoomItem[] = [];
    constructor(data?)
    {
        super('fruitBoomView',data);
    }

    public init()
    {
        this.setAnchor();
        this.setUI();
    }

    public setUI(data?)
    {
        
        if(data && (this.data && data.id != this.data.id) || !data)
        {  
            if(data)
            this.data = data;
            let id = this.data.id;
            this.fruit_left.source = 'fruit_left_'+id;
            this.fruit_right.source = 'fruit_right_'+id;
            let color = this.data.color;
            this.img_di.source = 'boom_di_'+color;
            this.img_boom.source = 'boom_'+color;
            this.boom_hide.source = 'boom_hide_'+color;
            this.img_kac.source = 'kac_'+color;
            this.addMc();
        }
    }

    public reset(data)
    {
        this.fruit_left.x = 28;
        this.fruit_left.y = 50;
        this.fruit_right.x = 100;
        this.fruit_right.y = 50;
        this.fruit_right.alpha = this.fruit_left.alpha = 1;
        this.fruit_left.rotation = this.fruit_right.rotation = 0;

        this.img_di.scaleX = this.img_di.scaleY = this.img_di.alpha =  this.img_kac.scaleX = 
        this.img_kac.scaleY = this.img_kac.alpha = this.img_boom.scaleX = this.img_boom.scaleY= 
        this.img_boom.alpha = 1;
        this.setUI(data);
    }

    public addMc()
    {
        let id = this.data.id;
        let color = this.data.color;
        this.dot_mc = null;
        this.dot_mc = MovieManager.instance().getFruitMovie('gif_'+color);
        if(!this.contains(this.dot_mc))
        this.addChildAt(this.dot_mc, 3);
        this.dot_mc.anchorOffsetX = this.dot_mc.width >> 1;
        this.dot_mc.anchorOffsetY = this.dot_mc.height >> 1;
        this.dot_mc.x = -(this.width >> 1) - 40;
        this.dot_mc.y = -(this.height >> 1) - 15;
        this.dot_mc.visible = false;
    }

    public boom(call:Function,scope)
    {
        this.visible = true;
        this.img_di.visible = true;
        this.img_boom.visible = true;
        this.boom_hide.visible = true;
        this.img_kac.visible = true;
        // this.dot_particle.visible = true;
        this.dot_mc.visible = true;

        let frame = 100/3;
        if(call)
        {
            call.call(scope);
        }

        egret.Tween.get(this.boom_hide).wait(frame).call(()=>{
            this.boom_hide.visible = false;
            // this.dot_particle.start();
            this.dot_mc.gotoAndPlay(0,1);
            // this.dot_mc.play(1);
        });
        this.img_di.scaleX
        egret.Tween.get(this.img_kac).to({
            alpha:0,
            scaleX:3.5,
            scaleY:3.5
        }, 15 * frame);
        egret.Tween.get(this.img_di).to({
            alpha:0,
            scaleX:3.5,
            scaleY:3.5
        },15 * frame);
        egret.Tween.get(this.img_boom).to({
            alpha:0,
            scaleX:3.5,
            scaleY:3.5
        },13 * frame);
        egret.Tween.get(this).call(()=>{
            this.fruit_left.visible = true;
            this.fruit_right.visible = true;
        }).to({factor:1}, 13 * frame);
        egret.Tween.get(this.fruit_left).to({
            rotation:-270,
            alpha:0
        },13 * frame);
        egret.Tween.get(this.fruit_right).to({
            rotation:90,
            alpha:0
        },13 * frame).call(()=>{
            // this.dot_particle.stop();
            // this.dot_particle.visible = false;
            this.dot_mc.visible = false;
            this.dot_mc.stop();
            this.visible = false;
            fruitBoomItem.boomItemPool.push(this);
        })
    }

    public get factor():number {
        return 0;
    }

    public set factor(value:number) {
        this.fruit_left.x = -((1 - value) * (1 - value) * 25 + 2 * value * (1 - value) * 50 + value * value * 110);
        this.fruit_left.y = (1 - value) * (1 - value) * 50 + 2 * value * (1 - value) * 0 + value * value * 300;

        this.fruit_right.x = (1 - value) * (1 - value) * 100 + 2 * value * (1 - value) * 125 + value * value * 185;
        this.fruit_right.y = (1 - value) * (1 - value) * 50 + 2 * value * (1 - value) * 0 + value * value * 300;
    }

    public clear()
    {
        super.clear();
        if(this.dot_mc)
        {
            this.dot_mc.stop();
            this.dot_mc = null;
        }
    }

    public static getBoomItem(data?)
    {
        let item:fruitBoomItem;
        let list = fruitBoomItem.boomItemPool;
        if(list.length > 0)
        {
            item = list.shift();
            item.reset(data);
        }
        else
        {
            item = new fruitBoomItem(data);
        }
        return item;
    }

    public static clearPool()
    {
        let list = fruitBoomItem.boomItemPool;
        if(list.length > 0)
        {
            for(let v of list)
            {
                v.remove();
                v = null;
            }
        }
        fruitBoomItem.boomItemPool = [];
    }
}